package com.tyros.web.log.aspect;

import com.tyros.web.log.HandlerLogProperties;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

/**
 * @author zhangshuaiyin
 * @date 2022/7/2 11:04
 */
@Slf4j
@Aspect
public class SwaggerAnnotationAspect {
    private final HandlerLogProperties properties;

    public SwaggerAnnotationAspect(HandlerLogProperties properties) {
        this.properties = properties;
    }

    @Around("@annotation(apiOperation)")
    public Object doAround(ProceedingJoinPoint joinPoint, ApiOperation apiOperation) throws Throwable {
        return AspectLogHelper.successLog(joinPoint, apiOperation, properties);
    }

    @AfterThrowing(throwing = "ex", pointcut = "@annotation(apiOperation)")
    public Object throwing(Throwable ex, ApiOperation apiOperation) {
        AspectLogHelper.throwingLog(ex, apiOperation, properties);
        return null;
    }
}
